package com.uyayo.blog.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.blog.bo.BlogBO;
import com.uyayo.blog.entity.Blog;
import com.uyayo.blog.entity.BlogIndustry;
import com.uyayo.blog.mapper.BlogIndustryMapper;
import com.uyayo.blog.mapper.BlogMapper;
import com.uyayo.blog.model.BlogCondition;
import com.uyayo.blog.model.BlogExCondition;
import com.uyayo.blog.service.BlogService;
import com.uyayo.framework.common.exception.CategoryNotFoundException;
import com.uyayo.framework.common.exception.ModifiedRecordNotExistException;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.*;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.pojo.PageOrderCondition;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("blogService")
public class BlogServiceImpl extends BaseServiceImpl<Blog> implements BlogService {

	private final BlogMapper blogMapper;

	private final BlogIndustryMapper blogIndustryMapper;

	public BlogServiceImpl(BlogMapper blogMapper, BlogIndustryMapper blogIndustryMapper) {
		this.blogMapper = blogMapper;
		this.blogIndustryMapper = blogIndustryMapper;
	}

	@Override
	public List<Blog> selectBlog(BlogCondition blogCondition) {
		Example example = new Example(Blog.class);
		example.createCriteria();
		return blogMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<Blog> selectBlogByPaging(Pageable<BlogCondition> pageable) {

		List<Blog> userList;
		//分页处理
		List<SortOrderMapping> sortOrderMappers = new ArrayList<>();
		sortOrderMappers.add(new SortOrderMapping("createTime", "create_time"));
		sortOrderMappers.add(new SortOrderMapping("updateTime", "update_time"));

		List<SortOrder> sortOrders = pageable.getSortOrders();
		String orderBy = SortOrderHelper.getOrderBy(sortOrderMappers, sortOrders);
		PageHelper.startPage(pageable.getCurrentPage(), pageable.getPageSize());
		
		Example example = new Example(Blog.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(pageable.getCondition().getTitle())){
			criteria.andLike("title","%"+pageable.getCondition().getTitle()+"%");
		}
		if(Objects.nonNull(pageable.getCondition().getMemberId())){
			criteria.andEqualTo("memberId",pageable.getCondition().getMemberId());
		}
		if(Objects.nonNull(pageable.getCondition().getBlogIndustryId())){
			criteria.andEqualTo("blogIndustryId",pageable.getCondition().getBlogIndustryId());
		}
		if(pageable.getCondition().getBeginCreateTime()!=null || pageable.getCondition().getEndCreateTime()!=null){
			criteria.andBetween("createTime", pageable.getCondition().getBeginCreateTime(), pageable.getCondition().getEndCreateTime());
		}
		userList = blogMapper.selectByExample(example);

		//取记录总条数
		PageInfo<Blog> pageInfo = new PageInfo<>(userList);
		return new PageBean<Blog>(pageInfo.getTotal(),userList);
	}

	@Override
	public PageBean<BlogBO> selectBlogByPaging(PageOrderCondition pageOrderCondition, BlogExCondition blogExCondition) {
		List<BlogBO> userList  = new ArrayList<>();
		//分页处理
		List<OrderBy> orderByList = new ArrayList<>();
		orderByList.add(new OrderBy("createTime", "create_time"));
		orderByList.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderByList, pageOrderCondition.getSort(), pageOrderCondition.getOrder());
		PageOrderByCondition pageOrderByCondition = new PageOrderByCondition();
		pageOrderByCondition.setLimit(pageOrderCondition.getLimit());
		pageOrderByCondition.setOffset(pageOrderCondition.getOffset());
		pageOrderByCondition.setOrderBy(orderBy);
		blogExCondition.setPageOrderByCondition(pageOrderByCondition);
		Integer totalCount = blogMapper.selectCountBlogByExample(blogExCondition);
		if(totalCount>0){
			userList = blogMapper.selectBlogByExample(blogExCondition);
		}
		return new PageBean<BlogBO>(totalCount,userList);
	}

	@Override
	public int saveBlog(Blog blog) throws CategoryNotFoundException {
		Long blogCategryId = blog.getBlogIndustryId();
		BlogIndustry blogIndustry = blogIndustryMapper.selectByPrimaryKey(blogCategryId);
		if(blogIndustry==null){
			throw new CategoryNotFoundException("参数异常，分类不存在，请刷新重试或联系管理员！");
		}
		Integer contentCount = blogIndustry.getBlogCount();
		blogIndustry.setBlogCount(contentCount+1);
		blogIndustryMapper.updateByPrimaryKeySelective(blogIndustry);
		return blogMapper.insertSelective(blog);
	}

	@Override
	public int updateBlog(Blog blog) throws CategoryNotFoundException, ModifiedRecordNotExistException {
		Long blogCategryId = blog.getBlogIndustryId();
		BlogIndustry blogIndustry = blogIndustryMapper.selectByPrimaryKey(blogCategryId);
		if(blogIndustry==null){
			throw new CategoryNotFoundException("参数异常，分类不存在，请刷新重试或联系管理员！");
		}

		Long blogId = blog.getId();
		Blog blogOld = blogMapper.selectByPrimaryKey(blogId);
		if(blogOld==null){
			throw new ModifiedRecordNotExistException("参数异常，要修改的记录不存在，请刷新重试或联系管理员！");
		}
		Long blogCategryIdOld = blogOld.getBlogIndustryId();

		if(!Objects.equals(blogCategryId,blogCategryIdOld)){
			Integer contentCount = blogIndustry.getBlogCount();
			blogIndustry.setBlogCount(contentCount+1);
			blogIndustryMapper.updateByPrimaryKeySelective(blogIndustry);

			BlogIndustry blogIndustryOld = blogIndustryMapper.selectByPrimaryKey(blogCategryIdOld);
			if(blogIndustryOld!=null && blogIndustryOld.getBlogCount()>0){
				Integer contentCountOld = blogIndustryOld.getBlogCount();
				blogIndustryOld.setId(blogCategryIdOld);
				blogIndustryOld.setBlogCount(contentCountOld-1);
				blogIndustryMapper.updateByPrimaryKeySelective(blogIndustryOld);
			}
		}
		return blogMapper.updateByPrimaryKeySelective(blog);
	}
}
